#!/usr/bin/make -f

pkg_name = temurin-11-jre
priority = 1112
jvm_tools = jaotc java jfr jjs jrunscript keytool pack200 rmid rmiregistry unpack200
amd64_tarball_url = https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.22%2B7/OpenJDK11U-jre_x64_linux_hotspot_11.0.22_7.tar.gz
amd64_checksum = 3a0fec1b9ef38d6abd86cf11f6001772b086096b6ec2588d2a02f1fa86b2b1de
arm64_tarball_url = https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.22%2B7/OpenJDK11U-jre_aarch64_linux_hotspot_11.0.22_7.tar.gz
arm64_checksum = 46e2bff7d5f419ac7c2fad29e78bfacf49ead4a2de1aba73b6329128f6d1f707
armhf_tarball_url = https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.22%2B7/OpenJDK11U-jre_arm_linux_hotspot_11.0.22_7.tar.gz
armhf_checksum = a5ab40aa53ecd413a8af738e66855d423e64b5389f876a4825e2cbdb45e9cfb3
ppc64el_tarball_url = https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.22%2B7/OpenJDK11U-jre_ppc64le_linux_hotspot_11.0.22_7.tar.gz
ppc64el_checksum = a6719f71217d0b6f931461acec465ca3a1eb0b0e94942fe165e27b30ecc341c2
s390x_tarball_url = https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.22%2B7/OpenJDK11U-jre_s390x_linux_hotspot_11.0.22_7.tar.gz
s390x_checksum = 1662e73deb814814fe27239666c5bf2d989484821343f0a3629ffb03729044ce

d = debian/$(pkg_name)
jvm_home = usr/lib/jvm
jvm_dir = $(pkg_name)-$(DEB_HOST_ARCH)
pkg_alias = $(pkg_name)-$(DEB_HOST_ARCH)

%:
	dh $@

override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
override_dh_strip:
override_dh_auto_install:
	# Download and verify checksum of JRE
	wget --progress=dot:mega -O jre.tar.gz $($(DEB_HOST_ARCH)_tarball_url)
	echo '$($(DEB_HOST_ARCH)_checksum) jre.tar.gz' > jre.tar.gz.sha256.txt
	sha256sum -c jre.tar.gz.sha256.txt

	# Substitute variables.
	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/jinfo.in > debian/jinfo

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/postinst.in > debian/postinst

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/prerm.in > debian/prerm

	# Unpack JRE.
	dh_installdirs \
		$(jvm_home) \
		$(jvm_home)/$(jvm_dir)
	tar --strip-components=1 -C "$(d)/$(jvm_home)/$(jvm_dir)" -xf jre.tar.gz

	# Add jinfo file (alternatives support).
	cp debian/jinfo "$(d)/$(jvm_home)/.$(pkg_alias).jinfo"

	# Replace bundled cacerts and redirect to adoptium-ca-certificates.
	rm -f "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"
	ln -s /etc/ssl/certs/adoptium/cacerts "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"

override_dh_gencontrol:
	dh_gencontrol --
